package com.angel.core.base;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 分页实体类
 * @author w.s
 *
 */
public class PageData<ENTITY> implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8689642433610091756L;

	private Logger log = LoggerFactory.getLogger(PageData.class);
	
	/**
	 * 当期分页的数据
	 */
	private List<ENTITY> currentDataList;
	/**
	 * 每页显示数据量
	 */
	private Integer pageSize;
	/**
	 * 分页后 总页数
	 */
	private Integer pageCount;
	/**
	 * 当前页码
	 */
	private Integer currentPageNum;
	/**
	 * 所有符合的记录条数
	 */
	private Integer totalCount;
	
	/**
	 * 分页条开始页码
	 */
	private Integer startNum;
	
	/**
	 * 分页条结束页码
	 */
	private Integer endNum;
	
	/**
	 * 分页系数  分页条中显示几个数字页码。
	 * 显示数字页码个数 = 2 * sideNum + 1
	 */
	private Integer sideNum = 4;
	

	/**
	 * 在构造分页对象的时候，进行总页数的计算
	 * @param currentDataList
	 * @param totalCount
	 * @param currentPageNum
	 * @param pageSize
	 */
	public PageData(List<ENTITY> currentDataList,Integer totalCount,Integer currentPageNum,Integer pageSize) {
		this.currentDataList = currentDataList;
		if(pageSize!=null){
			this.pageCount = (totalCount%pageSize==0) ? totalCount/pageSize : totalCount/pageSize+1;
		}else{
			this.pageCount = null;
		}		
		this.pageSize = pageSize;
		this.totalCount = totalCount;
		this.currentPageNum = currentPageNum;
		calcPage();
		log.debug("pageNum:["+this.currentPageNum+"],PageSize:["+this.pageSize+"]");
		log.debug("totalCount:["+this.totalCount+"],pageCount:["+this.pageCount+"]");
	}
	
	/**
	 * 计算分页
	 */
	private void calcPage(){
		if(pageCount<=sideNum){
			endNum = pageCount;
		}else{
			if((sideNum+currentPageNum)>=pageCount){
				endNum = pageCount;
			}else{
				endNum = sideNum+currentPageNum;
				if((sideNum+currentPageNum)<=(2*sideNum+1)){					
					if((2*sideNum+1)>=pageCount){
						endNum = pageCount;
					}else{
						endNum = 2*sideNum+1;
					}
				}else{
					endNum = sideNum + currentPageNum;
				}
			}
		}
		
		if(currentPageNum<=sideNum){
			startNum = 1;
		}else{			
			if((currentPageNum+sideNum)>=pageCount){
				if((2*sideNum+1)>=pageCount){
					if((pageCount - 2*sideNum)>=1){
						startNum = pageCount - 2*sideNum;
					}else{
						startNum = 1;
					}
				}else{
					startNum = pageCount - 2*sideNum;
				}				
			}else{
				if((currentPageNum-sideNum)>=1){
					startNum = currentPageNum - sideNum;
				}else{
					startNum = 1;
				}				
			}
		}
	}
	
	public List<ENTITY> getCurrentDataList() {
		return currentDataList;
	}
	
	
	public void setCurrentDataList(List<ENTITY> currentDataList) {
		this.currentDataList = currentDataList;
	}
	
	
	public Integer getPageSize() {
		return pageSize;
	}
	
	
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	
	public Integer getPageCount() {
		return pageCount;
	}
	
	
	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}


	public Integer getCurrentPageNum() {
		return currentPageNum;
	}


	public void setCurrentPageNum(Integer currentPageNum) {
		this.currentPageNum = currentPageNum;
	}
	

	public Integer getTotalCount() {
		return totalCount;
	}


	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getStartNum() {
		return startNum;
	}

	public void setStartNum(Integer startNum) {
		this.startNum = startNum;
	}

	public Integer getEndNum() {
		return endNum;
	}

	public void setEndNum(Integer endNum) {
		this.endNum = endNum;
	}

	public Integer getSideNum() {
		return sideNum;
	}

	public void setSideNum(Integer sideNum) {
		this.sideNum = sideNum;
	}
	
}
